//
//  HAHomeVC.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAHomeVC.h"
#import "HAHomeNoticeMo.h"
#import "SYReturnLabel.h"
#import "HARedpacketMo.h"
#import "HATips2PopView.h"
#import "HAWebVC.h"
#import "HAYJMo.h"
#import "HATransactionLookVC.h"
#import "HAPredationVC.h"
#import "WCScrollLabelView.h"

@interface HAHomeVC ()<UITableViewDataSource,UITableViewDelegate, WCScrollLabelViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) NSMutableArray *noticeMoList;
/***/
@property (nonatomic, strong) HAYJMo *dataModel;

@end

@implementation HAHomeVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self initData];
    self.baseNavView.hidden = YES;
    [self loadMenuData];
    self.view.backgroundColor = [UIColor khaBGColor];
    
    // Do any additional setup after loading the view.
}


- (void)initData {
    [self.dataArray removeAllObjects];
    [self.dataArray addObject:@"mod"];
    [self.tableView reloadData];
}

- (void)initUI{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-app_bottomH-49);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"mod"]) {
        CGFloat heiht = app_topH+20;
        if (self.noticeMoList.count > 0) {
            heiht += 38;
        }
        heiht += 602-127;
        return heiht;
    }
    return 0;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHHomeID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHHomeID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"mod"]) {
        [self getModCell:cell.contentView];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)scrollLabelView:(WCScrollLabelView *)scrollLabelView didClickAtIndex:(NSInteger)index
{
    HAHomeNoticeMo *mo = self.noticeMoList[index];
    NSString *tx = [NSString stringWithFormat:@"%@%@",mo.title,mo.content];
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[tx dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    NSString *t = [attrStr string];

    HATips2PopView *menuView = [[HATips2PopView alloc]initWithTitle:@"" content:t];
    [menuView show];
    menuView.XZYGTiShiSuretBlock = ^{};
}


-(void)loadData {
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestMethod = SYRequestMethodGET;
    request1.requestUrl = @"v1/agent/index";
    if (!self.dataModel) {
        [MBProgressHUD showLoading:self.view];
    }
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.code == 1) {
                self.dataModel = [HAYJMo mj_objectWithKeyValues:dataModel.data];
                [self.tableView reloadData];
            }
        }
    }];

}

-(void)loadMenuData {
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestMethod = SYRequestMethodGET;
    request1.requestUrl = @"index/notice";
    
    SYRequest *request2 = [[SYRequest alloc]init];
    request2.requestMethod = SYRequestMethodGET;
    request2.requestUrl = @"v1/notice/lists";
    
    SYRequest *request3 = [[SYRequest alloc]init];
    request3.requestMethod = SYRequestMethodGET;
    request3.requestUrl = @"v1/account/checkred";

    [[SYNetworking shareSingle] hgj_synRequestWithArray:@[request1,request2,request3] responseBlock:^(NSArray<SYDataModel *> *array) {
        
        if (array.count > 2) {
            SYDataModel *dataModel0 = array[0];
            SYDataModel *dataModel1 = array[1];
            SYDataModel *dataModel2 = array[2];

            HARedpacketMo *model1 = [HARedpacketMo mj_objectWithKeyValues:dataModel2.data];
            HAHomeNoticeMo *model2 = [HAHomeNoticeMo mj_objectWithKeyValues:dataModel0.data];
            
            [self.noticeMoList removeAllObjects];
            NSArray *array = [HAHomeNoticeMo mj_objectArrayWithKeyValuesArray:dataModel1.data];
            [self.noticeMoList addObjectsFromArray:array];
            [self.tableView reloadData];

            if (model1.count > 0) {
                HATips2PopView *menuView = [[HATips2PopView alloc]initWithTitle:@"通知" content:model1.msg];
                [menuView show];
                menuView.XZYGTiShiSuretBlock = ^{
                    if (model2.is_open == 1) {
                        [HASingle single].onlyMenu = [[HATipsPopView alloc]initWithTitle:@"通知" content:model2.text left:@"我知道了" right:@""];
                        [[HASingle single].onlyMenu show];          
                    }
                };
            }else {
                [HASingle single].onlyMenu = [[HATipsPopView alloc]initWithTitle:@"通知" content:model2.text left:@"我知道了" right:@""];
                [[HASingle single].onlyMenu show];
            }
        }
        
    }];

}


// @[@"交易明细",@"分润明细",@"快捷收款"]
-(void)item4Click:(UITapGestureRecognizer *)tap {
    if (tap.view.tag == 0) {
        HATransactionLookVC *vc = [[HATransactionLookVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag == 1) {
        HAPredationVC *vc = [[HAPredationVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag) {
        HAWebVC *vc = [[HAWebVC alloc]init];
        vc.tit = @"新手教程";
        vc.urlStr = [NSString stringWithFormat:@"%@index/article/index.html?type=3&token=%@",kHaBaseUrl,HAUserDefaults.token];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)item3Click:(UITapGestureRecognizer *)tap {
    HAWebVC *vc = [[HAWebVC alloc]init];
    if (tap.view.tag == 2) {
        vc.tit = @"领取红包";
        vc.urlStr = [NSString stringWithFormat:@"%@index/reward/reindex?token=%@",kHaBaseUrl,HAUserDefaults.token];
    }
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getModCell:(UIView *)contentView {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    contentView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F5"];
    
    UIImageView *topImageView = [UIImageView ha_createImageView];
    topImageView.userInteractionEnabled = YES;
    topImageView.image = [UIImage imageNamed:@"homebg"];
    [contentView addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_offset(360);
    }];
    
    CGFloat viewTop = app_topH+20;

    if (self.noticeMoList.count > 0) {
        
        UIView *d1View = [UIView ha_createView];
        d1View.layer.cornerRadius = 8;
        d1View.layer.masksToBounds = YES;
        d1View.backgroundColor = [UIColor whiteColor];
        [topImageView addSubview:d1View];
        d1View.frame = CGRectMake(15, viewTop, kScreenWidth-30, 48);
        
        UIImageView *_lImgV = [UIImageView ha_createImageView];
        _lImgV.image = [UIImage imageNamed:@"公告ic"];
        [d1View addSubview:_lImgV];
        _lImgV.frame = CGRectMake(14, 14, 20, 20);

        NSMutableArray *titles = [NSMutableArray array];
        for (HAHomeNoticeMo *mo in self.noticeMoList) {
            NSString *tx = [NSString stringWithFormat:@"%@%@",mo.title,mo.content];
            NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[tx dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
            
            NSString *t = [attrStr string];
            
            t = [t stringByReplacingOccurrencesOfString:@"\n" withString:@""];


            [titles addObject:t];
        }
        
        WCScrollLabelView *scrollLabelView = [[WCScrollLabelView alloc] initWithFrame:CGRectMake(42, 12, kUIScreen_w-30-42, 24)];
        scrollLabelView.backgroundColor = [UIColor whiteColor];
        scrollLabelView.delegate = self;
        scrollLabelView.isAttributedText = NO;
        scrollLabelView.titleArray = titles;
        //设置label的字体
        scrollLabelView.titleFont = [UIFont systemFontOfSize:14];
        //设置文字颜色
        scrollLabelView.titleColor = [UIColor redColor];
        //设置停留时间
        scrollLabelView.stayInterval = 3.f;
        //设置滚动动画时间
        scrollLabelView.animationDuration = 1.f;
        //设置对齐方式
        scrollLabelView.textAligment = NSTextAlignmentCenter;
        //设置内边距
        scrollLabelView.contentInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        [d1View addSubview:scrollLabelView];
        //开始滚动
        [scrollLabelView beginScrolling];

        viewTop += 48;
    }
    
    viewTop += 25;
    
    UIImageView *icbg0View = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yjic_m1"]];
    icbg0View.userInteractionEnabled = YES;
    [contentView addSubview:icbg0View];
    [icbg0View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewTop);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_offset(105);
    }];
    [icbg0View removeAllSubviews];
    NSArray *title0List = @[@"今日交易量", @"本月交易量"];
    NSMutableArray *view0List = [NSMutableArray array];
    for (int i = 0; i < title0List.count; i ++) {
        UIView *view = [UIView ha_createView];
        view.tag = i;
        
        UIView *icView = [UIView ha_createView];
        icView.layer.cornerRadius = 5;
        icView.layer.masksToBounds = YES;
        icView.backgroundColor = [UIColor colorWithHexString:@"#34A0FF"];
        [view addSubview:icView];
        [icView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(24);
            make.top.mas_equalTo(28);
            make.width.mas_offset(10);
            make.height.mas_offset(10);
        }];
        
        UILabel *titlabel = [UILabel ha_NLabelWithFont:16 textColor:@"#A2A2A2" text:title0List[i] alignment:0];
        [view addSubview:titlabel];
        [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(icView);
            make.left.equalTo(icView.mas_right).offset(8);
        }];
        
        UILabel *conlabel = [UILabel ha_BLabelWithFont:24 textColor:@"#222C35" text:@"0" alignment:0];
        [view addSubview:conlabel];
        [conlabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(titlabel.mas_bottom).offset(16);
            make.left.mas_equalTo(titlabel);
            make.right.mas_equalTo(0);
        }];
        [icbg0View addSubview:view];
        [view0List addObject:view];
        
        if (i == 0 && ![NSString isEmptyString:self.dataModel.tradeDayTotal]) {
            conlabel.text = self.dataModel.tradeDayTotal;
        }
        if (i == 1 && ![NSString isEmptyString:self.dataModel.tradeMonthTotal]) {
            conlabel.text = self.dataModel.tradeMonthTotal;
        }
    }
    
    [view0List mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:(kScreenWidth-30)*0.5 leadSpacing:0 tailSpacing:0];
    [view0List mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(105);
        make.centerY.mas_equalTo(0);
    }];

    viewTop += 105+10;

    UIImageView *icbg1View = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yjic_m2"]];
    icbg1View.userInteractionEnabled = YES;
    [contentView addSubview:icbg1View];
    [icbg1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewTop);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_offset(105);
    }];
    [icbg1View removeAllSubviews];
    NSArray *title1List = @[@"今日分润（元）", @"本月分润"];
    NSMutableArray *view1List = [NSMutableArray array];
    for (int i = 0; i < title1List.count; i ++) {
        UIView *view = [UIView ha_createView];
        view.tag = i;
        
        UIView *icView = [UIView ha_createView];
        icView.layer.cornerRadius = 5;
        icView.layer.masksToBounds = YES;
        icView.backgroundColor = [UIColor colorWithHexString:@"#FF9A8D"];
        [view addSubview:icView];
        [icView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(24);
            make.top.mas_equalTo(28);
            make.width.mas_offset(10);
            make.height.mas_offset(10);
        }];
        
        UILabel *titlabel = [UILabel ha_NLabelWithFont:16 textColor:@"#A2A2A2" text:title1List[i] alignment:0];
        [view addSubview:titlabel];
        [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(icView);
            make.left.equalTo(icView.mas_right).offset(8);
        }];
        
        UILabel *conlabel = [UILabel ha_BLabelWithFont:24 textColor:@"#222C35" text:@"0" alignment:0];
        [view addSubview:conlabel];
        [conlabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(titlabel.mas_bottom).offset(16);
            make.left.mas_equalTo(titlabel);
            make.right.mas_equalTo(0);
        }];
        [icbg1View addSubview:view];
        [view1List addObject:view];
        
        if (i == 0 && ![NSString isEmptyString:self.dataModel.shareDayTotal]) {
            conlabel.text = self.dataModel.shareDayTotal;
        }
        if (i == 1 && ![NSString isEmptyString:self.dataModel.shareMonthTotal]) {
            conlabel.text = self.dataModel.shareMonthTotal;
        }
    }
    
    [view1List mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:(kScreenWidth-30)*0.5 leadSpacing:0 tailSpacing:0];
    [view1List mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(105);
        make.centerY.mas_equalTo(0);
    }];

    viewTop += 105+10;

    UIView *main5View = [UIView ha_createView];
    main5View.backgroundColor = [UIColor whiteColor];
    main5View.layer.cornerRadius = 8;
    main5View.layer.masksToBounds = YES;
    [contentView addSubview:main5View];
    [main5View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewTop);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(104);
    }];
    
    [main5View removeAllSubviews];
    
    NSArray *titles = @[@"交易明细",@"分润明细",@"新手教程"];
    NSArray *images = @[@"ha_yjic1",@"ha_yjic2",@"ha_新手教程"];
    
    NSMutableArray *view5List = [NSMutableArray array];
    for (int i = 0; i < titles.count; i ++) {
        UIView *view = [UIView ha_createView];
        view.backgroundColor = [UIColor whiteColor];
        view.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(item4Click:)];
        [view addGestureRecognizer:tap];
        
        UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:images[i]]];
        [view addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(10);
            make.width.mas_offset(32);
            make.height.mas_offset(32);
        }];
        
        UILabel *label = [UILabel ha_NLabelWithFont:14 textColor:@"#222C35" text:titles[i] alignment:1];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(icon.mas_bottom).offset(8);
            make.right.left.equalTo(view);
        }];
        
        [main5View addSubview:view];
        [view5List addObject:view];
        
        if (HAUserDefaults.isLock == 1 && i == 1) {
            view.hidden = YES;
        }
    }
    
    CGFloat spacing = 15;
    CGFloat tiemW = (kScreenWidth-spacing*2)/3;
    
    [view5List mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:tiemW leadSpacing:spacing tailSpacing:spacing];
    [view5List mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(80);
        make.top.mas_equalTo(12);
    }];

    viewTop += 104+25;

    UIImageView *img3View = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ham领取红包"]];
    img3View.userInteractionEnabled = YES;
    img3View.tag = 2;
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(item3Click:)];
    [img3View addGestureRecognizer:tap3];
    [contentView addSubview:img3View];
    [img3View.superview bringSubviewToFront:img3View];
    
    [img3View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(viewTop);
        make.height.mas_offset(114);
    }];  
    
    if (HAUserDefaults.isLock == 1) {
        img3View.hidden = YES;
    }

}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F5"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}

- (NSMutableArray *)noticeMoList
{
    if (!_noticeMoList) {
        _noticeMoList = [NSMutableArray array];
        
    }
    return _noticeMoList;
}

@end
